#include "../LogicProtocol_CommHeader.h"
#include "LogicProtocol_CommPacket.h"
CLogicProtocol_CommPacket::CLogicProtocol_CommPacket()
{
}
CLogicProtocol_CommPacket::~CLogicProtocol_CommPacket()
{
}
//////////////////////////////////////////////////////////////////////////
//                       公有函数
//////////////////////////////////////////////////////////////////////////
BOOL CLogicProtocol_CommPacket::LogicProtocol_CommPacket_Hdr(NETENGINE_PROTOCOLHDR *pSt_ProtocolHdr, TCHAR *ptszMsgBuffer, int *pInt_Len)
{
    LogicProtocol_IsErrorOccur = FALSE;

    if ((NULL == pSt_ProtocolHdr) || (NULL == ptszMsgBuffer) || (NULL == pInt_Len))
    {
        LogicProtocol_IsErrorOccur = TRUE;
        LogicProtocol_dwErrorCode = ERROR_WINAPT_LOGIC_PROTOCOL_COMM_PACKETHDR_PARAMENT;
        return FALSE;
    }
    pSt_ProtocolHdr->unPacketSize = 0;
    *pInt_Len = sizeof(NETENGINE_PROTOCOLHDR);
    memcpy(ptszMsgBuffer,pSt_ProtocolHdr,sizeof(NETENGINE_PROTOCOLHDR));

    return TRUE;
}
BOOL CLogicProtocol_CommPacket::LogicProtocol_CommPacket_HdrJson(NETENGINE_PROTOCOLHDR *pSt_ProtocolHdr, TCHAR *ptszMsgBuffer, int *pInt_Len,int nMsgCode, LPCTSTR lpszMsgJson)
{
    LogicProtocol_IsErrorOccur = FALSE;

    if ((NULL == pSt_ProtocolHdr) || (NULL == ptszMsgBuffer) || (NULL == pInt_Len))
    {
        LogicProtocol_IsErrorOccur = TRUE;
        LogicProtocol_dwErrorCode = ERROR_WINAPT_LOGIC_PROTOCOL_COMM_PKTJSONHDR_PARAMENT;
        return FALSE;
    }

    Json::Value st_JsonRoot;
    st_JsonRoot["Code"] = nMsgCode;
    if (NULL != lpszMsgJson)
    {
        st_JsonRoot["Msg"] = lpszMsgJson;
    }
    pSt_ProtocolHdr->unPacketSize = st_JsonRoot.toStyledString().length();

    *pInt_Len = sizeof(NETENGINE_PROTOCOLHDR) + pSt_ProtocolHdr->unPacketSize;
    memcpy(ptszMsgBuffer,pSt_ProtocolHdr,sizeof(NETENGINE_PROTOCOLHDR));
    memcpy(ptszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR),st_JsonRoot.toStyledString().c_str(),pSt_ProtocolHdr->unPacketSize);

    return TRUE;
}

